﻿package eu.liquify.ui {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	public class UIContainer extends UIItem {
		public static var childAliases:Object = { };
		
		protected var content:Sprite = new Sprite();
		
		public var autosize:Boolean = true;
		public var autosizeCascade:Boolean = false;
		
		public function UIContainer(options:Object = null) {
			super(options);
			addRawChild(content);
		}

		/*
		override public function serialize():String {
			var s:String = super.serialize();
			for (var i:int = 0; i < numChildren; i++) {
				var item:UIItem = getChildAt(i) as UIItem;
				if (item && item.serializable) {
					var serialized:String = item.serialize();
					if (serialized.length > 0) {
						var container:Boolean = item is UIContainer;
						
						if (!container) s += item.label+"=";
						s += serialized;
						if (!container) s += "\n";
					}
				}
			}
			return s;
		}
		override public function deserialize(s:String):void {
			super.deserialize(s);
			
		}
		*/
		
		public function getValues():Object {
			var values:Object = { };
			for (var i:int = 0; i < numChildren; i++) {
				var d:DisplayObject = getChildAt(i);
				if (d is IUIValue) {
					var uiv:IUIValue = d as IUIValue;
					values[uiv.uiName] = uiv.value;
				}
			}
			return values;
		}
		
		
		public function addRawChild(child:DisplayObject):DisplayObject {
			return super.addChild(child);
		}
		
		public function removeRawChild(child:DisplayObject):DisplayObject {
			return super.removeChild(child);
		}
		/*
		private function childReposition(e:Event):void {
			var oldWidth:Number = width;
			var oldHeight:Number = height;
			reposition();
			//if (oldWidth != width || oldHeight != height) dispatchEvent(e);
		}
		
		protected function reposition():void {}
		*/
		
		override public function addChild(child:DisplayObject):DisplayObject {
			//child.addEventListener(UIItem.REPOSITION, childReposition);
			return content.addChild(child);
		}
		public function addChildVector(children:Vector.<DisplayObject>):Vector.<DisplayObject> {
			var l:int = children.length;
			for (var i:int = 0; i < l; i++) {
				addChild(children[i]);
			}
			return children;
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject {
			//child.addEventListener(UIItem.REPOSITION, childReposition);
			return content.addChildAt(child, index);
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject {
			//child.removeEventListener(UIItem.REPOSITION, childReposition);
			return content.removeChild(child);
		}
		public function removeChildVector(children:Vector.<DisplayObject>):Vector.<DisplayObject> {
			var l:int = children.length;
			for (var i:int = 0; i < l; i++) {
				removeChild(children[i]);
			}
			return children;
		}
		
		override public function removeChildAt(index:int):DisplayObject {
			var child:DisplayObject = content.removeChildAt(index);
			//child.removeEventListener(UIItem.REPOSITION, childReposition);
			return child;
		}
		
		override public function getChildAt(index:int):DisplayObject {
			return content.getChildAt(index);
		}
		
		override public function getChildByName(name:String):DisplayObject {
			return content.getChildByName(name);
		}
		
		override public function getChildIndex(child:DisplayObject):int {
			return content.getChildIndex(child);
		}
		
		override public function setChildIndex(child:DisplayObject, index:int):void {
			content.setChildIndex(child, index);
		}
		
		override public function get numChildren():int { 
			return content.numChildren;
		}
		
		override public function contains(child:DisplayObject):Boolean {
			return content.contains(child);
		}
		
	}
}